package com.odontosis.view.alterarsenha;

import java.util.ArrayList;
import java.util.Collection;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.odontosis.entidade.Usuario;
import com.odontosis.service.UsuarioService;
import com.odontosis.view.OdontosisAction;

public class AcaoAlterarSenha extends OdontosisAction<UsuarioService> {
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		FormAlterarSenha formulario = (FormAlterarSenha) form;
		if(StringUtils.isEmpty(formulario.getMetodo()))
		{
			return onIniciarTela(mapping, form, request, response);
		}
		return onAlterarSenha(mapping, form, request, response);
	}
	
	public ActionForward onIniciarTela(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		return mapping.getInputForward();
	}
	
	public ActionForward onAlterarSenha(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		FormAlterarSenha formulario = (FormAlterarSenha) form;
		Usuario usuario = service.buscarPorLogin(formulario.getLogin());
		usuario.setSenha(formulario.getNovaSenha());
		service.update(usuario);
		
		formulario.resetar();
		Collection<String> sucesso = new ArrayList<String>();
		sucesso.add("Senha Alterada com Sucesso");
		request.setAttribute("sucesso", sucesso);
		request.setAttribute("mensagens", null);

		return mapping.getInputForward();
	}
}
